package com.jiazhong.mingxing.project.bookstore.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiazhong.mingxing.project.bookstore.commons.bean.Book;
import com.jiazhong.mingxing.project.bookstore.commons.mapper.BookMapper;
import com.jiazhong.mingxing.project.bookstore.commons.util.JsonResult;
import com.jiazhong.mingxing.project.bookstore.commons.util.ResultTool;
import com.jiazhong.mingxing.project.bookstore.front.service.BookService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Cacheable(cacheNames = "findLowerPriceBook", key = "#size")
    public JsonResult findLowerPriceBook(int size) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        // 1.排序
        queryWrapper.orderByAsc("price");
        // 2.只取前四本
        queryWrapper.last(" limit " + size);
        return ResultTool.success(list(queryWrapper));
    }

    @Cacheable(cacheNames = "findMoreBooksByCategoryId", key = "#categoryId")
    public JsonResult findMoreBooksByCategoryId(String categoryId) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        // 按照分类ID获取到图书
        queryWrapper.eq("category_id", categoryId);
        List<Book> list = list(queryWrapper);
        return ResultTool.success(list);
    }

    @Cacheable(cacheNames = "findMoreBooksByBookName",key = "#bookName")
    public JsonResult findMoreBooksByBookName(String bookName) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", bookName);
        List<Book> list = list(queryWrapper);
        return ResultTool.success(list);
    }
}
